﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Aspose.Words.Rendering;

namespace Helpers
{
    public static class WordHelper
    {

        /// <summary> 获取 Word 每一页的高度 此项获得整个页面</summary>
        /// <param name="pageinfo"></param>
        /// <returns></returns>
        private static int GetWordHeight(PageInfo pageinfo)
        {
            var flag = BindingFlags.NonPublic | BindingFlags.Instance;
            // 经过反编译看到 Word 中 PageInfo 的高度属性被混淆为 Int32_1
            var pageheight = pageinfo.GetType().GetProperty("Int32_1", flag);
            if (pageheight != null)
            {
                var height = pageheight.GetValue(pageinfo, null);
                if (height != null)
                {
                    return (int)height;
                }
            }

            return (int)pageinfo.WidthInPoints;
        }

        /// <summary> 获取 Word 每一页的宽度 此项获得整个页面 </summary>
        /// <param name="pageinfo"></param>
        /// <returns></returns>
        private static int GetWordWidth(PageInfo pageinfo)
        {
            var flag = BindingFlags.NonPublic | BindingFlags.Instance;
            // 经过反编译看到 Word 中 PageInfo 的宽度属性被混淆为 Int32_0
            var pagewidth = pageinfo.GetType().GetProperty("Int32_0", flag);
            if (pagewidth != null)
            {
                var width = pagewidth.GetValue(pageinfo, null);
                if (width != null)
                {
                    return (int)width;
                }
            }

            return (int)pageinfo.WidthInPoints;
        }

        /// <summary> 转换 Word 的每一页为图片 </summary>
        /// <param name="wordfile"></param>
        /// <param name="savedirectory"></param>
        /// <returns></returns>
        public static List<string> ConvertToImages(string wordfile, string savedirectory = "")
        {
            // 返回的图片绝对路径集合
            List<string> images = new List<string>();

            // 获取 Word 文件名称
            var wordname = System.IO.Path.GetFileNameWithoutExtension(wordfile);

            // 如果指定了保存路径，则使用传入的路径，否则在 Word 文件的同级创建同名文件夹当作保存路径
            savedirectory = string.IsNullOrWhiteSpace(savedirectory) ? System.IO.Path.GetDirectoryName(wordfile) : savedirectory;
            savedirectory = System.IO.Path.Combine(savedirectory, wordname);
            // 防止保存的路径不存在因此创建保存文件夹
            Directory.CreateDirectory(savedirectory);

            var doc = new Aspose.Words.Document(wordfile);

            int count = doc.PageCount;
            // Aspose.Word 的单页获取索引从 0 开始
            for (int i = 0; i < count; i++)
            {
                var imagefile = System.IO.Path.Combine(savedirectory, i + ".jpg");

                PageInfo pageInfo = doc.GetPageInfo(i);
                var size = pageInfo.GetSizeInPixels(1, 96);
                int width = size.Width;
                int height = size.Height;

                using (var img = new Bitmap(width, height))
                {
                    using (var gr = Graphics.FromImage(img))
                    {
                        gr.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                        gr.FillRectangle(System.Drawing.Brushes.White, 0, 0, width, height);
                        doc.RenderToSize(i, gr, 0, 0, width, height);
                    }

                    // 生成图像
                    img.Save(imagefile);
                    images.Add(imagefile);
                }
            }

            return images;
        }

        /// <summary> 把 Word 转换为 Pdf </summary>
        /// <param name="wordfile"></param>
        /// <param name="pdffile"></param>
        /// <returns></returns>
        public static bool ConvertWordToPdf(string wordfile, string pdffile)
        {
            try
            {
                var doc = new Aspose.Words.Document(wordfile);
                doc.Save(pdffile, Aspose.Words.SaveFormat.Pdf);
                return true;
            }
            catch { }
            return false;
        }

    }
}
